﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************
namespace BitFlex.Windows.Forms {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections.ObjectModel;

    /// <summary>
    /// Collection used primarily for storing <see cref="WizardPage"/> controls, but is extensible and supports designer interaction
    /// </summary>
    /// <typeparam name="TWizardPage">The type of <see cref="WizardPage"/> items in the collection</typeparam>
    [Serializable()]
    public class WizardPageCollection<TWizardPage> : Collection<TWizardPage> where TWizardPage : WizardPage {

        #region Fields

        /// <devdoc>
        /// Internal Field,
        /// Used to store what <see cref="Wizard"/> control owns this collection of pages. This
        /// field is used to notify the wizard of changes to the list so the changes are reflected
        /// in the designer, and at run-time.
        /// 
        /// Example:
        /// WizardPageList.Add(new WizardPage()),
        /// This will notify the parent wizard to display the newly added page and add it to it's
        /// control collection
        /// </devdoc>
        internal Wizard OwnerWizard;
        
        #endregion

        #region Methods

        /// <summary>
        /// Overrides <see cref="List<T>.Add"/> and adds the <see cref="TWizardPage"/> to the collection's parent wizard
        /// </summary>
        /// <param name="item">The item to add</param>
        new public void Add(TWizardPage item) {
            if (!base.Contains(item)) {
                base.Add(item);
                if (OwnerWizard != null && !OwnerWizard.Controls.Contains(item)) {
                    OwnerWizard.Controls.Add(item);
                }
                item.Dock = DockStyle.Fill;
                item.Visible = false;
            }
        }

        /// <summary>
        /// Overrides <see cref="List<T>.Insert"/> and inserts the <see cref="TWizardPage"/> to the collection's parent wizard
        /// </summary>
        /// <param name="index">The index at which to insert the item</param>
        /// <param name="item">The item to insert</param>
        new public void Insert(int index, TWizardPage item) {
            if (!base.Contains(item)) {
                base.Insert(index, item);
                if (OwnerWizard != null && !OwnerWizard.Controls.Contains(item)) {
                    OwnerWizard.Controls.Add(item);
                }
                item.Dock = DockStyle.Fill;
                item.Visible = false;
            }
        }

        /// <summary>
        /// Overrides <see cref="List<T>.Remove"/> and removes the <see cref="TWizardPage"/> from the collection's parent wizard
        /// </summary>
        /// <param name="item">The item to remove</param>
        new public bool Remove(TWizardPage item) {
            if (base.Contains(item)) {
                bool removed = base.Remove(item);
                if (OwnerWizard != null && OwnerWizard.Controls.Contains(item)) {
                    OwnerWizard.Controls.Remove(item);
                }
                return removed;
            }
            else {
                return false;
            }
        }

        #endregion
    }
}